using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace TowerDefence_QF
{
	public class MainInterfaceData : UIPanelData
	{
	}
	public partial class MainInterface : UIPanel,IController
	{
		private PathSystem mPathSystem;
		
		private LevelSystem mLevelSystem;
		protected override void OnInit(IUIData uiData = null)
		{
			mData = uiData as MainInterfaceData ?? new MainInterfaceData();
			
			mPathSystem = this.GetSystem<PathSystem>();
			mLevelSystem=this.GetSystem<LevelSystem>();
			
			//初始化注册事件
			StartGameBtn.onClick.AddListener(OnClickStartGameBtn);
			BgBtn.onClick.AddListener(OnClickBgBtn);
			SoundBtn.onClick.AddListener(OnClickSoundBtn);
			//获取Bg 声音是否开启
			var isOnBg = AudioKit.Settings.IsMusicOn.Value;
			var isOnSound = AudioKit.Settings.IsSoundOn.Value;
			//根据是否开启，设置文本
			var isOnBgText=isOnBg?"On":"Off";
			var isOnSoundText=isOnSound?"On":"Off";
			//初始化文本
			BgText.text = $"BG：{isOnBgText}";
			SoundText.text=$"Sound：{isOnSoundText}";

		}
		
		protected override void OnOpen(IUIData uiData = null)
		{
		}
		
		protected override void OnShow()
		{
		}
		
		protected override void OnHide()
		{
		}
		
		protected override void OnClose()
		{
			mPathSystem=null;
			//关闭面板时，移除事件
			StartGameBtn.onClick.RemoveListener(OnClickStartGameBtn);
			BgBtn.onClick.RemoveListener(OnClickBgBtn);
			SoundBtn.onClick.RemoveListener(OnClickSoundBtn);
		}
		/// <summary>
		/// 点击开始游戏事件
		/// </summary>
		private void OnClickStartGameBtn()
		{
			
			mPathSystem.GeneratePath(0);
			mLevelSystem.SetLevel(1);
			UIKit.OpenPanel<GamePanel>();
			this.CloseSelf();
		}
		/// <summary>
		/// 点击背景音乐事件
		/// </summary>
		private void OnClickBgBtn()
		{
			//获取bg的开关状态，取反
			var isOn = !AudioKit.Settings.IsMusicOn.Value;
			AudioKit.Settings.IsMusicOn.Value = isOn;
			//设置文本
			var isOnText=isOn?"On":"Off";
			BgText.text = $"BG：{isOnText}";

		}
		/// <summary>
		/// 点击声明事件
		/// </summary>
		private void OnClickSoundBtn()
		{
			//获取sound的开关状态，取反
			var isOn =! AudioKit.Settings.IsSoundOn.Value;
			AudioKit.Settings.IsSoundOn.Value = isOn;
			//设置文本
			var isOnText=isOn?"On":"Off";
			SoundText.text = $"Sound：{isOnText}";
		}

		public IArchitecture GetArchitecture()
		{
			return GameApp.Interface;
		}
	}
}
